

import ShSdk from './shsdk';
import GameData from './gamedata';

const defaultLocalModel = {
    version: 1,
    music: 1,  // 背景开关
    sound: 1,  // 音效开关

    levelData: [
        {
            lv: 1,
            star: 0,
            score: 0,
        }
    ], // 关卡数据
};

const defaultPlayerModel = {
   coin:100, // 金币
};


let localModel,playerModel;
let bNeedResetData = false;


// ShSdk.config = {
//     bannerAdId:'',
//     intersisterAdId:'',
//     videoAdId:'',
//     configFile:'zuma.json',
//     saveType:GameData.Type.NO_NETWORK,
//     httpUrl:'',
// };

// ShSdk.dyConfig = {
//     bannerAdId:'',
//     intersisterAdId:'',
//     videoAdId:'',
//     configFile:'zuma_dy.json',
//     saveType:GameData.Type.NO_NETWORK,
//     httpUrl:'',
// };


ShSdk.init(defaultLocalModel,defaultPlayerModel,()=>{

    // 检查每日更新的数据
    GameData.dailyDataCheck();

    // 1. 判断是否需要重置数据
    if(bNeedResetData){
        GameData.resetData();
    }

    // 2. 检查数据正确性
    // TODO:
    localModel = GameData.getLocalModel();
    playerModel = GameData.getPlayerModel();

    // 把这个数据同步给自己的

    onUserDataLoaded();
});

function onUserDataLoaded() {
    // TODO: 加载游戏资源

}

function _fix_levelData(localModel,canPlayMaxLevel){

    let levelData = localModel.levelData;
    if(levelData.length > canPlayMaxLevel){
        levelData.length = canPlayMaxLevel;
        let lastItem = levelData[levelData.length - 1];
        lastItem.star = 0;
        lastItem.score = 0;
    }else{
        // <
        let added = canPlayMaxLevel - levelData.length;
        for(let i = 0; i < added;++i){
            if(i === added - 1){
                levelData.push({
                    lv:i + 1,
                    star:0,
                    score:0,
                });
            }else{
                levelData.push({
                    lv:i + 1,
                    star:3,
                    score:myTools.random(60000,80000),
                });
            }
        }
    }
}

function CheckLevelData(){
    let canPlayMaxLevel = G.playerModel.getCanPlayMaxLevel();
    if(G.localModel.levelData.length !== canPlayMaxLevel){
        _fix_levelData(G.localModel,canPlayMaxLevel);
    }
}
